package com.zsy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zsy.entity.PurchaseRequirements;
import com.zsy.mapper.IPurchaseRequirementsMapper;

import com.zsy.service.IPurchaseRequirementsService;
import com.zsy.utils.PageBean;
import com.zsy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurchaseRequirementsServiceImpl implements IPurchaseRequirementsService {

    @Autowired
    private IPurchaseRequirementsMapper IPRM;

    /**
     * 分页查询
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param relatedTo 关联项目
     * @param initiatingDepartment 发起部门
     * @param itemName 物品名称
     * @return
     */
    @Override
    public PageBean<PurchaseRequirements> list(Integer pageNum, Integer pageSize,
                                               String relatedTo, String initiatingDepartment, String itemName) {

        // 1.创建PageBean对象
        PageBean<PurchaseRequirements> pb = new PageBean<>();
        // 2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);
        // 3.调用mapper
        //Map<String,Object> map = ThreadLocalUtil.get();
        //Integer userId = (Integer) map.get("id");
        List<PurchaseRequirements> as = IPRM.list(1,relatedTo, initiatingDepartment, itemName);
        // Page中提供了方法，可获取PageHelper分页查询后，得到的总条数和当前页数据
        Page<PurchaseRequirements> p = (Page<PurchaseRequirements>) as;
        // 把数据填到PageBean中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    public Result deleteById(Integer id){
        if (id == null){
            return Result.error("id为空");
        }
        int i = IPRM.deleteById(id);
        System.out.println("i"+i);
        if (i>0){
            return Result.ok("删除成功") ;
        }
        return Result.error("删除失败");
    }


    public Result update(PurchaseRequirements pr) {
        if(pr.getPurchaseRequirementsId()<0){
            return Result.error("id为空");
        }
        int update = IPRM.update(pr);
        if (update > 0){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }
}
